home *** CD-ROM | disk | FTP | other *** search
/ Sun Solutions 2000 #2 / Sun Solutions CD (Volume 2 2000)(Special Focus - Java Technologies)(Disc 1).ISO / products / Software / TeamQuestCorporation / _install / install.sh < prev    next >
Linux/UNIX/POSIX Shell Script  |  1999-05-06  |  12KB  |  530 lines

  1. #!/bin/ksh
  2. #
  3. #  Copyright (c) 1998 TeamQuest Corporation.
  4. #  All Rights Reserved.
  5. #
  6. # $Id: lite-install.sh,v 1.1 1998/10/09 15:52:15 aeg Exp $
  7. #
  8.  
  9. # set the LANG variable so that we can parse output of commands
  10. LANG=C
  11. export LANG
  12.  
  13. # setup characters for carriage return suppress on echo statements
  14. if [ `echo -n | wc -c` = 0 ] ; then
  15.    # BSD style
  16.    fore=-n
  17.    aft=
  18. else
  19.    # SYSV style
  20.    fore=
  21.    aft="\c"
  22. fi
  23.  
  24. yes() {
  25.    ans="$1"
  26.    if [ -z "$ans" ] ; then
  27.       read ans
  28.    else
  29.       echo "$ans"
  30.    fi
  31.    ans=${ans:-$2}
  32.    case "$ans" in
  33.       [Yy]*) return 0 ;;
  34.    esac
  35.    return 1
  36. }
  37.  
  38. is_user_root() {
  39.    if [ -z "`id | grep 'uid=0(' `" ] ; then
  40.       echo $fore "
  41. Warning: Since you are not running as root, you may not have enough
  42. privilege to successfully ${1:-install} TeamQuest Lite. Do you wish to
  43. continue? [y,N] $aft"
  44.       yes "" N || exit 1
  45.    fi
  46. }
  47.  
  48. addf() {
  49.    # contents file
  50.    CONTENTS=$TARGETDIR/etc/contents
  51.    [ -f $CONTENTS ] || > $CONTENTS
  52.  
  53.    PKGNAME=$1
  54.    FILELIST=$2
  55.  
  56.    # add the new records, keeping them all in sorted order
  57.    sort -u $CONTENTS $FILELIST |
  58.    awk '
  59.       NF == 1 {
  60.          if (newf) {print newf, pkgname}
  61.          newf = $1
  62.          next
  63.       }
  64.       $1 == newf {
  65.          n = 0;
  66.          for (i = 2; i <= NF; i++)
  67.             if ($i == pkgname) {
  68.                n++;
  69.             }
  70.          if (n == 0)
  71.             print $0, pkgname
  72.          else 
  73.             print
  74.          newf=""
  75.          next
  76.       }
  77.       newf != "" { print newf, pkgname; newf="" }
  78.       { print }
  79.       END { if (newf) print newf, pkgname }
  80.    ' pkgname="$PKGNAME" > /tmp/content$$ &&
  81.    cp /tmp/content$$ $CONTENTS &&
  82.    rm -f /tmp/content$$
  83. }
  84.  
  85. # usage: install_files targetdir pkgname
  86. install_files() {
  87.    TARGETDIR=$1
  88.    PKGNAME=$2
  89.  
  90.    # Determine tarfile
  91.    tarfile=tqlite.gz
  92.  
  93.    # See if we can read the tarfile
  94.    if [ ! -r $tarfile ] ; then
  95.       echo ""
  96.       echo "Unable to read $tarfile to install TeamQuest Lite"
  97.       echo ""
  98.       return 1
  99.    fi
  100.  
  101.    # Determine how to unpack the tarfile
  102.    if [ -x ./gunzip ] ; then
  103.       unpack="./gunzip -dc"
  104.    elif LOCATABLE gunzip ; then
  105.       unpack="gunzip -dc"
  106.    elif LOCATABLE gzip ; then
  107.       unpack="gzip -dc"
  108.    else
  109.       echo ""
  110.       echo "Unable to locate gunzip to unpack $tarfile"
  111.       echo ""
  112.       return 1
  113.    fi
  114.  
  115.    extractlog=/tmp/newf$$
  116.    > $extractlog
  117.  
  118.    echo ""
  119.    echo $fore "Extracting files to $TARGETDIR ...$aft"
  120.    $unpack < $tarfile | (cd $TARGETDIR; tar xovf - ) > $extractlog 2>&1
  121.    echo " Extraction done."
  122.    echo ""
  123.  
  124.    # prepare list of new files just installed
  125.    # derive it from tar xv logfile
  126.    awk '/^x .*, / { print substr($2,1,length($2)-1) }
  127.         /linked to/ {print $1}
  128.         /symbolic link to/ {print $2}' $extractlog | sort > /tmp/newfs$$
  129.  
  130.    # add the new files to the contents database
  131.    addf $PKGNAME /tmp/newfs$$
  132.    rm -f $extractlog /tmp/newfs$$
  133.    return 0
  134. }
  135.  
  136. # function to detect if package has already been installed
  137. already_installed() {
  138.  
  139.    unset PRESENT
  140.    [ -r $TARGETDIR/bin/tqlite ] && PRESENT=$TARGETDIR/etc/tqdaemon
  141.    [ -r $TESTROOT/etc/init.d/tqlite ] && PRESENT=$TESTROOT/etc/init.d/tqlite
  142.  
  143.    [ -z "$PRESENT" ] && return 1
  144.  
  145.    echo ""
  146.    echo "TeamQuest Lite has already been installed."
  147.    echo $fore "Do you want to re-install TeamQuest Lite? [y,N] $aft"
  148.    if yes "" N ; then
  149.       # attempt to stop the previous daemons
  150.       # if not successful, return 0 to indicate no reinstall
  151.       ksh $PRESENT stop || return 0
  152.       return 1
  153.    fi
  154.    return 0
  155. }
  156.  
  157. install_lite() {
  158.  
  159.    echo ""
  160.    echo "Installation of TeamQuest Lite begins..."
  161.  
  162.    set_targetdir || return 1
  163.  
  164.    # Check to see if the package is already installed
  165.    if already_installed $TARGETDIR ; then
  166.       return 1
  167.    fi
  168.  
  169.    # Install the files
  170.    if install_files $TARGETDIR tqlite ; then
  171.  
  172.       # locate install.sh and make a copy as remove.sh
  173.       # INSTFILE=install.sh
  174.       if [ -f "$INSTFILE" ] ; then
  175.          rm -f $TARGETDIR/etc/remove.sh 2>/dev/null
  176.          cp "$INSTFILE" $TARGETDIR/etc/remove.sh
  177.          chmod 755 $TARGETDIR/etc/remove.sh 2>/dev/null
  178.       fi
  179.  
  180.       # do postinstall handling
  181.       $TARGETDIR/etc/tqdaemon install
  182.       $TARGETDIR/etc/tqdaemon start
  183.  
  184.       # print completion message
  185.       echo ""
  186.       echo "Installation of TeamQuest Lite complete."
  187.       if [ "record" = "$INSTMODE" ] ; then
  188.          echo ""
  189.          echo "Responses to prompts have been recorded in $TQRESPONSE."
  190.       fi
  191.       echo ""
  192.       echo "You may use $TARGETDIR/etc/remove.sh to"
  193.       echo "remove TeamQuest Lite at a later time."
  194.       echo ""
  195.       return 0
  196.    else
  197.       return 1
  198.    fi
  199. }
  200.  
  201. LOCATABLE() {
  202.    type "$1" > /dev/null 2>&1
  203. }
  204.  
  205. # function to ask user for the directory where packages
  206. # are to be installed.
  207. set_targetdir() {
  208.    default_answer=/opt/tqlite
  209.    if [ -n "$TARGETDIR" ] ; then
  210.       default_answer=$TARGETDIR
  211.    fi
  212.  
  213.    if [ -r $TESTROOT/etc/init.d/tqlite ] ; then
  214.       default_answer=`sed -n -e 's/^ *//' -e 's:/etc/tqdaemon start::p' $TESTROOT/etc/init.d/tqlite`
  215.    fi
  216.  
  217.    while [ "silent" != "$INSTMODE" ] ; do
  218.  
  219.       echo $fore "
  220. Enter the pathname of the directory where TeamQuest Lite
  221. is to be installed.  [$default_answer] $aft"
  222.  
  223.       read ans
  224.       ans=${ans:-$default_answer}
  225.       ans=`eval echo $ans`
  226.  
  227.       # if the directory doesn't exist, try to make it
  228.       [ -d $ans ] || mkdir $ans
  229.       # if the directory exists or we made it, set the TARGETDIR
  230.       # value to escape the while loop
  231.       [ -d $ans ] && TARGETDIR=$ans && break
  232.    done
  233.  
  234.    # if the target directory doesn't exist, try to make it and
  235.    # return from the function if it can't be created.
  236.    [ -d $TARGETDIR ] || mkdir $TARGETDIR || return 1
  237.  
  238.    # Check on etc directory
  239.    [ -d $TARGETDIR/etc ] || mkdir $TARGETDIR/etc || return 1
  240.    if [ -w $TARGETDIR/etc ] ; then
  241.       return 0
  242.    else
  243.       echo "Unable to write to directory $TARGETDIR/etc"
  244.       return 1
  245.    fi
  246. }
  247.  
  248. display_license() {
  249.    if [ -z "$1" ] ; then
  250.       return
  251.    fi
  252.  
  253.    if [ ! -f "$1" ] ; then
  254.       echo " "
  255.       echo "ERROR: Cannot find license file ... aborting"
  256.       echo " "
  257.       exit 1
  258.    fi
  259.  
  260.    echo $fore "
  261. Before installation begins, please read the following License
  262. Agreement.
  263.  
  264. Continue? [y] $aft"
  265.    read ans
  266.    case "$ans" in
  267.       [Nn]*)
  268.          exit 1 ;;
  269.    esac
  270.  
  271.    if LOCATABLE "$PAGER" ; then
  272.       : # ok
  273.    elif LOCATABLE more ; then
  274.       PAGER=more
  275.    elif LOCATABLE pg ; then
  276.       PAGER=pg
  277.    else
  278.       PAGER=cat
  279.    fi
  280.  
  281.    echo " "
  282.    $PAGER "$1"
  283.  
  284.    ans=
  285.    while [ -z "$ans" ] ; do
  286.       echo $fore "
  287.  
  288. To accept the terms and conditions of this agreement enter \"accept\".
  289. To decline the terms and conditions of this agreement enter \"decline\".
  290.  
  291. Do you accept the terms and conditions of this license agreement? $aft"
  292.       read ans
  293.       ans=`echo "$ans" | tr '[a-z]' '[A-Z]'`
  294.       case "$ans" in
  295.       ACCEPT)
  296.          ;;
  297.       DECLINE)
  298.          echo " "
  299.          echo "License not accepted ... aborting installation"
  300.          echo " "
  301.          exit 1
  302.          ;;
  303.       *)
  304.          echo " "
  305.          echo "ERROR: Invalid response, expected \"accept\" or \"decline\" ... try again"
  306.          ans=
  307.          ;;
  308.       esac
  309.    done
  310. }
  311.  
  312. do_preremove() {
  313.  
  314.    $TARGETDIR/etc/tqdaemon remove
  315.    $TARGETDIR/etc/tqdaemon stop
  316.  
  317.    LOGDIR=`sed -n 's/^Log Directory *= *//p' $TARGETDIR/etc/tq.ini`
  318.    LOGDIR=${LOGDIR:=/tmp/teamquest}
  319.    DATADIR=`sed -n 's/^Data Directory *= *//p' $TARGETDIR/etc/tq.ini`
  320.  
  321.    (
  322.    echo "$LOGDIR" 
  323.    for f in tqbnp tqbsp tqtcpd tqwarp ; do
  324.       echo "$LOGDIR/$f.log"
  325.    done
  326.    find $DATADIR
  327.    ) > /tmp/fl$$
  328.  
  329.    addf tqlite /tmp/fl$$
  330.    rm -f /tmp/fl$$
  331. }
  332.  
  333. # usage: remove_pkg pkgname 
  334. remove_pkg() {
  335.    PKGNAME=$1
  336.  
  337.    if [ ! -w $TARGETDIR/etc ] ; then
  338.       echo "Unable to write to directory $TARGETDIR/etc"
  339.       return 1
  340.    fi
  341.  
  342.    if [ ! -w $TARGETDIR/etc/contents ] ; then
  343.       echo "Unable to update $TARGETDIR/etc/contents"
  344.       return 1
  345.    fi
  346.  
  347.    cd $TARGETDIR
  348.  
  349.    if do_preremove ; then
  350.       remove_files
  351.       return $?
  352.    fi
  353.    return 0
  354. }
  355.  
  356. remove_files() {
  357.    # contents file
  358.    CONTENTS=$TARGETDIR/etc/contents
  359.  
  360.    awk '{
  361.       n = 0;
  362.       for (i = 2; i <= NF; i++)
  363.          if ($i == pkgname) {
  364.             n++;
  365.          }
  366.       if (n == 0)
  367.          print
  368.       else if (NF-n == 1) {
  369.          print $1 > rmlist
  370.       }
  371.       else {
  372.          # print the revised contents line
  373.          ORS=""
  374.          print $1;
  375.          for (i = 2; i <= NF; i++)
  376.             if ($i != pkgname)
  377.                print " " $i
  378.          print "\n"
  379.          ORS="\n"
  380.       }
  381.    }
  382.    ' pkgname=$PKGNAME rmlist=/tmp/rml$$ $CONTENTS > /tmp/content$$ &&
  383.    cp /tmp/content$$ $CONTENTS
  384.    rmdlist=/tmp/rmd$$
  385.    > $rmdlist
  386.  
  387.    # set up a remove logfile
  388.    logfile=/tmp/${USER}-rm.log
  389.    i=0
  390.    while [ -f $logfile ] ; do
  391.       logfile="/tmp/${USER}-rm${i}.log"
  392.       i=`expr $i + 1`
  393.    done
  394.  
  395.    echo "Logging files and directories removed to $logfile"
  396.    echo "Begin removing files at `date`" >> $logfile
  397.  
  398.    for f in `cat /tmp/rml$$` ; do
  399.       if [ -d $f ] ; then
  400.          echo $f >> $rmdlist
  401.          continue
  402.       fi
  403.       dirname $f >> $rmdlist
  404.       echo "Removing $f" >> $logfile
  405.       echo $fore ".$aft"
  406.       if rm -f $f ; then
  407.          :
  408.       else
  409.          echo "Failed on $f" >> $logfile
  410.       fi
  411.    done
  412.    for f in `sort -ru $rmdlist` ; do
  413.       case "$f" in
  414.          /*) continue ;;
  415.       esac
  416.       dn=`dirname $f`
  417.       until [ "$dn" = "." ] ; do
  418.          echo $dn
  419.          dn=`dirname $dn`
  420.       done
  421.    done > $rmdlist.1
  422.    cat $rmdlist.1 >> $rmdlist
  423.    for f in `sort -ru $rmdlist` ; do
  424.       [ -d $f ] || continue
  425.       [ "$f" = "etc" ] && continue # skip etc here, we'll get it later
  426.       echo "Removing $f" >> $logfile
  427.       echo $fore ".$aft"
  428.       rmdir $f
  429.    done
  430.    rm -f /tmp/content$$ /tmp/rml$$ $rmdlist $rmdlist.1
  431.    rm -f $TARGETDIR/etc/$PKGNAME.pkg
  432.    if [ ! -s $CONTENTS ] ; then
  433.       echo "Removing $CONTENTS" >> $logfile
  434.       echo $fore ".$aft"
  435.       rm -f $CONTENTS
  436.       echo "Removing $TARGETDIR/etc/remove.sh" >> $logfile
  437.       echo $fore ".$aft"
  438.       rm -f $TARGETDIR/etc/remove.sh
  439.       echo "Removing $TARGETDIR/etc" >> $logfile
  440.       echo $fore ".$aft"
  441.       rmdir $TARGETDIR/etc
  442.       echo "Removing $TARGETDIR" >> $logfile
  443.       echo $fore ".$aft"
  444.       cd ..
  445.       rmdir $TARGETDIR
  446.    fi
  447.  
  448.    echo ""
  449.    echo "Done removing files"
  450.    echo "Done removing files at `date`" >> $logfile
  451. }
  452.  
  453. remove_lite() {
  454.    echo ""
  455.    echo "Removal of TeamQuest Lite begins..."
  456.    echo $fore "
  457. Are you sure you want to completely remove TeamQuest Lite
  458. and all of its components? [Y,n] $aft"
  459.    if yes "" Y ; then
  460.       remove_pkg tqlite
  461.    else
  462.       echo ""
  463.       echo "Removal of TeamQuest Lite cancelled"
  464.    fi
  465. }
  466.  
  467. # begin mainline of the script
  468.  
  469. func=`basename $0 .sh`
  470. INSTFILE="$0"
  471. unset RESPARG INSTMODE NOPT TARGETDIR
  472. while getopts "nxd:r:s:" c ; do
  473.    case "$c" in
  474.       r)
  475.          if [ -n "$INSTMODE" ] ; then
  476.             echo "-r and -s options are mutually exclusive"
  477.             exit 1
  478.          fi
  479.          INSTMODE="record"
  480.          RESPARG=$OPTARG
  481.          ;;
  482.       s)
  483.          if [ -n "$INSTMODE" ] ; then
  484.             echo "-r and -s options are mutually exclusive"
  485.             exit 1
  486.          fi
  487.          INSTMODE="silent"
  488.          NOPT="accept"
  489.          RESPARG=$OPTARG
  490.          ;;
  491.       n) NOPT="accept" ;;
  492.       d) TARGETDIR="$OPTARG" ;;
  493.       x) set -x ;;
  494.       \?)
  495.          echo "$0 [-r response] [pkg...]"
  496.          echo ""
  497.          echo "$0 [-s response] [pkg...]"
  498.          exit 1
  499.          ;;
  500.    esac
  501. done
  502. if [ -z "$OPTIND" ] ; then
  503.    # getopts is misbehaving; perhaps this is a non-posix Bourne shell on OSF1
  504.    # feed ourselves to ksh which should behave correctly
  505.    # there is no return from exec
  506.    exec ksh "$0" "$@"
  507. fi
  508. shift `expr $OPTIND - 1`
  509.  
  510. if [ "$func" != "remove" ] ; then
  511.    is_user_root install
  512.    test "$NOPT" = "accept" ||
  513.    display_license license.txt
  514.    install_lite || {
  515.       echo ""
  516.       echo "Installation of TeamQuest Lite failed."
  517.       echo ""
  518.       exit 1
  519.    }
  520. else
  521.    # remove is found in TARGETDIR/etc
  522.    TARGETDIR=`cd \`dirname $0\`/..; /bin/pwd`
  523.    if [ "$1" = "addf" ] ; then
  524.       addf $2 $3
  525.    else
  526.       is_user_root remove
  527.       remove_lite
  528.    fi
  529. fi
  530.